Laravel / Controller / Steps to create a controller
Step to create controller
-
STEP
1. Create Controller in app/Http/Controllers
2. We can create multiple functions inside a controller
3. we can create multiple Controllers inside the controller folder1. Naming Rule
Controllers should be in PascalCase/CapitalCase. They should be in singular case, no spacing between words, and end with "Controller". Also, each word should be capitalised (i.e. BlogController, not blogcontroller). For example: BlogController, AuthController, UserController.
2. Steps
1. create the controller StudentController.php in app/Http/Controllers folder
2. add namespace namespace App\Http\Controllers;
3. import base class of laravel controller use App\Http\Controllers\Controller;
4. extend the Controller class from Controller using extend keyword;
namespace App\Http\Controllers; use App\Http\Controllers\Controller; class StudentController extends Controller { } 3. define variables and functions
namespace App\Http\Controllers; use App\Http\Controllers\Controller; class StudentController extends Controller { public $mark; public function findGrade(){ echo 1; } public function findFee(){ echo 2; } } 4. load view page
return view() is used for loading the view page. first parameter of view() is name of the view blade in resources/views
public function findGrade(){ return view('blade_name'); }